﻿package mortal.game.view.copy.towerCopy.panel
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.debug.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.towerCopy.cellrender.*;
    import mortal.game.view.task.view.base.*;
    import mortal.mvc.core.*;

    public class TowerTaskInfoPanel extends TaskViewContentPanel
    {
        private var hasShowFinishAlert:Boolean;
        private var bgPass:GBitmap;
        private var itemList:GTileList;
        private var txtLeftTime:BitmapNumberText;
        private var bgRound:GBitmap;
        private var _clip:MovieClip;
        private var bgKill:GBitmap;
        private var startTime:Number;
        private var timerLeft:Timer;
        private var calLeftTime:Number;
        private var txtTitle:GTextFiled;
        private var imgTarget:GBitmap;
        private var imgPass:GBitmap;
        private var imgHLine1:GBitmap;
        private var imgHLine2:GBitmap;
        private var textbg0:GBitmap;
        private var textbg1:GBitmap;
        private var textbg2:GBitmap;
        private var bgLeftTime:GBitmap;
        private var txtSecond:GTextFiled;
        private var txtList:Vector.<GTextFiled>;
        private var txtAddTime:GTextFiled;
        private var bgAddTimeSec:GBitmap;
        private var bgAddTimeSec2:GBitmap;
        private var pannel:GSprite;
        private var pannelY0:Number;
        private var pannelAddTime:GSprite;
        private const txtListStartY:int = 51;
        private var _curLv:int = -1;

        public function TowerTaskInfoPanel()
        {
            return;
        }// end function

        private function passToNextLayer() : void
        {
            var _loc_1:* = SceneConfig.instance.getSceneInfo(Game.sceneInfo.sMapDefine.mapId);
            var _loc_2:* = _loc_1.passInfos[0] as SPassPoint;
            var _loc_3:* = _loc_2.passTo[0] as SPassTo;
            var _loc_4:* = new Point();
            _loc_4.x = _loc_2.point.x;
            _loc_4.y = _loc_2.point.y;
            AIManager.cancelAll();
            AIManager.addMoveTo(_loc_4, 0);
            Log.error(Language.getString(71003) + "4");
            AIManager.addPassToMap(_loc_2);
            AIManager.start();
            return;
        }// end function

        private function onAddEntity(event:DataEvent) : void
        {
            this.refresh();
            return;
        }// end function

        override public function refresh(param1:Boolean = false) : void
        {
            var passFinish:Function;
            var reload:* = param1;
            super.refresh(reload);
            if (!allowRefresh)
            {
                return;
            }
            var copyInfo:* = Cache.instance.copy.tower.towerCopyInfo;
            var copyCode:* = Cache.instance.copy.copyInfo.code;
            var configKills:* = TowerCopyConfig.instance.getPassConditionByID(copyCode, Cache.instance.copy.tower.getCurrentLevel(copyCode));
            if (configKills)
            {
                this.reCreateTxtBossList(configKills);
            }
            this.itemList.dataProvider = Cache.instance.copy.tower.getCurrentPassRewardsDataProvider(4, copyCode);
            this.itemList.drawNow();
            if (copyInfo && copyInfo.level != this._curLv)
            {
                this._curLv = copyInfo.level;
                this.calLeftTime = copyInfo.surplusSeconds;
                if (!this.timerLeft.running && copyInfo.result == false)
                {
                    this.startTime = getTimer();
                    this.timerLeft.start();
                }
                this.txtLeftTime.text = copyInfo.surplusSeconds.toString();
            }
            if (copyInfo && copyInfo.result)
            {
                this.itemList.dataProvider = this.getEmptyDataProvider(this.itemList.length);
                this.itemList.drawNow();
                if (GameConst.TConstTowerMaxLevel == Cache.instance.copy.tower.getPlayerMaxPass(copyCode) && !this.hasShowFinishAlert)
                {
                    this.hasShowFinishAlert = true;
                    passFinish = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    GameProxy.copy.leaveCopy();
                }
                return;
            }// end function
            ;
                    Alert.show("" + Language.getString(71004), null, Alert.OK | Alert.CANCEL, null, passFinish);
                }
                this.txtTitle.htmlText = TowerCopyConfig.instance.getInfoByID(copyCode, copyInfo.level).levelName;
                this.imgPass.visible = true;
                if (Cache.instance.copy.tower.towerPassMsg)
                {
                }
            }
            else
            {
                if (copyInfo && !copyInfo.result)
                {
                }
                this.imgPass.visible = false;
                this.pannelAddTime.visible = false;
                this.txtTitle.htmlText = TowerCopyConfig.instance.getInfoByID(copyCode, Cache.instance.copy.tower.getCurrentLevel(copyCode)).levelName;
            }
            return;
        }// end function

        private function timerHandler(event:TimerEvent) : void
        {
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:int = 0;
            var _loc_2:* = this.calLeftTime - (getTimer() - this.startTime) / 1000 + 1;
            var _loc_3:* = _loc_2 / 60;
            var _loc_4:* = int(_loc_2 % 60);
            if (_loc_2 <= 0)
            {
                this.txtLeftTime.text = "0";
                this.timerLeft.stop();
            }
            else if (_loc_3 == 0 && _loc_4 < 10)
            {
                _loc_8 = _loc_2 - int(_loc_2);
                if (_loc_8 >= 0.5)
                {
                    this.txtLeftTime.text = int(_loc_2) + "";
                    this.txtLeftTime.filters = [FilterConst.colorFilterRed];
                }
                else
                {
                    this.txtLeftTime.text = int(_loc_2) + "";
                    this.txtLeftTime.filters = [];
                }
            }
            else
            {
                this.txtLeftTime.text = int(_loc_2) + "";
                this.txtLeftTime.filters = [];
            }
            var _loc_5:* = Cache.instance.copy.tower.towerCopyInfo;
            var _loc_6:* = TowerCopyConfig.instance.getInfoByID(_loc_5.copyCode, _loc_5.level);
            var _loc_7:* = _loc_6.limitSeconds;
            if (this._clip && _loc_5)
            {
                _loc_9 = _loc_2;
                _loc_9 = _loc_7 - _loc_9;
                _loc_9 = _loc_9 / _loc_7;
                _loc_9 = _loc_9 * 100;
                _loc_10 = int(_loc_9);
                this._clip.gotoAndStop(_loc_10);
            }
            return;
        }// end function

        override public function get titleLabel() : GTextFiled
        {
            return this.txtTitle;
        }// end function

        private function reCreateTxtBossList(param1:Vector.<TowerKillInfo>, param2:Dictionary = null) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:GTextFiled = null;
            var _loc_7:int = 0;
            var _loc_8:EntityDictionary = null;
            var _loc_9:IEntity = null;
            var _loc_10:String = null;
            var _loc_11:String = null;
            var _loc_3:* = this.txtList.length;
            if (_loc_3 > 0)
            {
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    this.txtList[_loc_4].dispose();
                    _loc_4++;
                }
                this.txtList.length = 0;
            }
            _loc_3 = param1.length;
            _loc_5 = this.txtListStartY;
            _loc_4 = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_6 = UIFactory.gTextField("" + Language.getString(71005), 50, 45, 100, 20, this, null, true);
                _loc_6.configEventListener(TextEvent.LINK, this.clickBossLink);
                this.txtList.push(_loc_6);
                _loc_6.y = _loc_5;
                _loc_5 = _loc_5 + (_loc_6.textHeight + 5);
                _loc_7 = 1;
                _loc_8 = ThingUtil.entityUtil.entitysMap;
                for each (_loc_9 in _loc_8.allEntitys)
                {
                    
                    if (_loc_9 is MonsterPlayer && MonsterPlayer(_loc_9).bossInfo.code == param1[_loc_4].BossID)
                    {
                        _loc_7 = 0;
                        break;
                    }
                }
                _loc_10 = HTMLUtil.addLink(param1[_loc_4].BossName, GlobalStyle.colorLv);
                _loc_11 = HTMLUtil.addColor(_loc_7 + "/" + param1[_loc_4].BossNum, GlobalStyle.colorHuang2);
                _loc_6.htmlText = _loc_10 + "   " + _loc_11;
                _loc_6.Data = param1[_loc_4].BossID;
                this.txtList[_loc_4].parent.swapChildren(this.txtList[_loc_4], this.imgPass);
                _loc_4++;
            }
            this.pannel.y = this.pannelY0 + _loc_5 - 45;
            return;
        }// end function

        private function getEmptyDataProvider(param1:int) : DataProvider
        {
            var _loc_2:* = new DataProvider();
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2.addItem(new Object());
                _loc_3++;
            }
            return _loc_2;
        }// end function

        private function clickBossLink(event:TextEvent) : void
        {
            var _loc_2:* = String(GTextFiled(event.currentTarget).Data);
            fightBoss(_loc_2);
            return;
        }// end function

        private function onTowerPublicTowerPass(param1:Object) : void
        {
            this.timerLeft.stop();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.txtTitle = UIFactory.gTextField("" + Language.getString(71006), 75, 3, 100, 18, this, GlobalStyle.textFormatAnjin, true);
            this.textbg0 = UIFactory.bitmap(ImagesConst.tower_view_textbg, 5, 19, this);
            this.imgTarget = UIFactory.gBitmap(ImagesConst.tower_view_target, 10, 32, this);
            this.bgKill = UIFactory.gBitmap(ImagesConst.tower_view_kill, 10, 53, this);
            this.txtList = new Vector.<GTextFiled>;
            this.imgPass = UIFactory.gBitmap(ImagesConst.tower_view_pass, 85, 72, this);
            this.imgPass.rotation = -35;
            this.imgPass.visible = false;
            this.pannel = UICompomentPool.getUICompoment(GSprite) as GSprite;
            addChild(this.pannel);
            var _loc_1:int = -40;
            this.pannel.y = -40;
            this.pannelY0 = _loc_1;
            this.imgHLine1 = UIFactory.bitmap(ImagesConst.tower_win_hLine, 10, 90, this.pannel);
            this.textbg1 = UIFactory.bitmap(ImagesConst.tower_view_textbg, 0, 87, this.pannel);
            this.bgPass = UIFactory.bitmap(ImagesConst.tower_view_text, 10, 100, this.pannel);
            this.itemList = UIFactory.tileList(10, 125, 270, 43, this.pannel);
            this.itemList.rowHeight = 42;
            this.itemList.columnWidth = 42;
            this.itemList.horizontalGap = 1;
            this.itemList.verticalGap = 1;
            this.itemList.rowCount = 6;
            this.itemList.setStyle("cellRenderer", TowerRewardsCellRenderer);
            this.imgHLine2 = UIFactory.bitmap(ImagesConst.tower_win_hLine, 10, 172, this.pannel);
            this.textbg2 = UIFactory.bitmap(ImagesConst.tower_view_textbg, 0, 172, this.pannel);
            this.bgLeftTime = UIFactory.bitmap(ImagesConst.tower_view_time, 10, 185, this.pannel);
            this.bgRound = UIFactory.bitmap(ImagesConst.tower_view_round, 8, 205, this.pannel);
            this.txtLeftTime = UIFactory.bitmapNumberText(98, 265, "GuildCopyNum.png", 24, 32, -6, this.pannel, 9);
            this.txtLeftTime.align = BitmapNumberText.Mid;
            this.txtSecond = UIFactory.gTextField("<font size=\'22\'>S</font>", 88, 297, 160, 60, this.pannel, GlobalStyle.textFormatBai, true);
            this.pannelAddTime = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this.pannel.addChild(this.pannelAddTime);
            this.pannelAddTime.visible = false;
            this.bgAddTimeSec = UIFactory.bitmap(ImagesConst.tower_view_s_add, 2, 212, this.pannelAddTime);
            this.txtAddTime = UIFactory.gTextField("<font color=\'" + GlobalStyle.colorLv + "\'>555</font>", 33, 209, 40, 60, this.pannelAddTime, null, true);
            this.txtAddTime.autoSize = TextFieldAutoSize.CENTER;
            this.bgAddTimeSec2 = UIFactory.bitmap(ImagesConst.tower_view_s, 65, 215, this.pannelAddTime);
            this.timerLeft = new Timer(500);
            this.timerLeft.addEventListener("timer", this.timerHandler);
            this.hasShowFinishAlert = false;
            LoaderManager.instance.load("ChallengeTimeClock" + ".swf", this.resGotHandler);
            Dispatcher.addEventListener(EventName.Scene_AddEntity, this.onAddEntity);
            NetDispatcher.addCmdListener(ServerCommand.TowerPublicTowerPass2, this.onTowerPublicTowerPass);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            var _loc_3:int = 0;
            super.disposeImpl(param1);
            Dispatcher.removeEventListener(EventName.Scene_AddEntity, this.onAddEntity);
            NetDispatcher.removeCmdListener(ServerCommand.TowerPublicTowerPass2, this.onTowerPublicTowerPass);
            this.timerLeft.removeEventListener("timer", this.timerHandler);
            this.timerLeft = null;
            this._curLv = -1;
            this.bgPass = this.bgPass && this.bgPass.dispose(param1) ? (null) : (null);
            this.itemList = this.itemList && this.itemList.dispose(param1) ? (null) : (null);
            this.txtLeftTime = this.txtLeftTime && this.txtLeftTime.dispose(param1) ? (null) : (null);
            this.txtAddTime = this.txtAddTime && this.txtAddTime.dispose(param1) ? (null) : (null);
            this.bgAddTimeSec = this.bgAddTimeSec && this.bgAddTimeSec.dispose(param1) ? (null) : (null);
            this.bgAddTimeSec2 = this.bgAddTimeSec2 && this.bgAddTimeSec2.dispose(param1) ? (null) : (null);
            this.bgRound = this.bgRound && this.bgRound.dispose(param1) ? (null) : (null);
            this.txtTitle = this.txtTitle && this.txtTitle.dispose(param1) ? (null) : (null);
            this.imgTarget = this.imgTarget && this.imgTarget.dispose(param1) ? (null) : (null);
            this.imgPass = this.imgPass && this.imgPass.dispose(param1) ? (null) : (null);
            this.bgLeftTime = this.bgLeftTime && this.bgLeftTime.dispose(param1) ? (null) : (null);
            this.bgKill = this.bgKill && this.bgKill.dispose(param1) ? (null) : (null);
            if (this._clip)
            {
                this._clip.stop();
                DisplayUtil.removeMe(this._clip);
                this._clip = null;
            }
            this.imgHLine1 = this.imgHLine1 && this.imgHLine1.dispose(param1) ? (null) : (null);
            this.imgHLine2 = this.imgHLine2 && this.imgHLine2.dispose(param1) ? (null) : (null);
            this.textbg0 = this.textbg0 && this.textbg0.dispose(param1) ? (null) : (null);
            this.textbg1 = this.textbg1 && this.textbg1.dispose(param1) ? (null) : (null);
            this.textbg2 = this.textbg2 && this.textbg2.dispose(param1) ? (null) : (null);
            this.txtSecond = this.txtSecond && this.txtSecond.dispose(param1) ? (null) : (null);
            this.pannelAddTime = this.pannelAddTime && this.pannelAddTime.dispose(param1) ? (null) : (null);
            this.pannel = this.pannel && this.pannel.dispose(param1) ? (null) : (null);
            var _loc_2:* = this.txtList ? (this.txtList.length) : (0);
            if (_loc_2 > 0)
            {
                _loc_3 = 0;
                while (_loc_3 < _loc_2)
                {
                    
                    this.txtList[_loc_3].dispose(param1);
                    _loc_3++;
                }
                this.txtList.length = 0;
            }
            this.txtList = null;
            this.hasShowFinishAlert = false;
            return;
        }// end function

        private function resGotHandler(param1:SWFInfo) : void
        {
            if (_disposed)
            {
                return;
            }
            this._clip = param1.clip;
            this._clip.x = -66;
            this._clip.y = 110;
            this.pannel.addChild(this._clip);
            return;
        }// end function

        private static function fightBoss(param1:String) : void
        {
            var _loc_2:* = new Dictionary();
            _loc_2[param1] = true;
            AIManager.cancelAll();
            AIManager.addAutoFight(-1, _loc_2);
            AIManager.start();
            return;
        }// end function

    }
}
